package com.example.jiancaiwang.fragment;


import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jiancaiwang.Api;
import com.example.jiancaiwang.R;
import com.example.jiancaiwang.adapter.HomeMenuAdapter;
import com.example.jiancaiwang.controller.HomeController;
import com.example.jiancaiwang.entity.BannerEmntity;
import com.example.jiancaiwang.entity.MenuEntity;
import com.example.jiancaiwang.view.IView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment implements IView {
    private HomeController homeController;
    private Banner banner;
    private RecyclerView recyclerView;
    private List<MenuEntity> list  = new ArrayList<>();
    private HomeMenuAdapter homeMenuAdapter;
    private PullToRefreshLayout pullToRefreshLayout;
    public HomeFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        banner = view.findViewById(R.id.banner);
        recyclerView = view.findViewById(R.id.rv);
        homeMenuAdapter = new HomeMenuAdapter(R.layout.item_home_menu,list);
        recyclerView.setAdapter(homeMenuAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
        homeController = new HomeController(this);
        homeController.getBannerData();
        homeController.getMenuData();
        homeController.getTypeData();
        return view;
    }
    @Override
    public void onOk(String json,int type) {
        if(type == Api.TYPE_HOME_BANNER){//轮播图展示
            //解析数组
            Gson gson = new Gson();
            Type type1 = new TypeToken<List<BannerEmntity>>() {
            }.getType();
            List<BannerEmntity> list = gson.fromJson(json,type1);
            banner.setImages(list);
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    BannerEmntity bannerEmntity = (BannerEmntity) path;
                    String image_src = bannerEmntity.getImage_src();
                    Glide.with(context).load(image_src).into(imageView);
                }
            });
            banner.start();



        }else if(type == Api.TYPE_HOME_MENU){//中间菜单

            Gson gson = new Gson();
            Type type1 = new TypeToken<List<MenuEntity>>() {
            }.getType();

            List<MenuEntity> menuEntities = gson.fromJson(json,type1);
            list.addAll(menuEntities);
            homeMenuAdapter.notifyDataSetChanged();

        }else if(type == Api.TYPE_HOME_TYPE){//分类展示

        }
    }
    @Override
    public void onError() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }
}
